home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / dfÜ / online / online2ncomm / online2ncomm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-16  |  5.7 KB  |  239 lines

  1. /*                            Online-o-meter
  2.  *                   Online Timer/Cost Meter for Amiga 
  3.  *                      © E.F.Pritchard 1994,1995.
  4.  *                              *FREEWARE*
  5.  *        See Online-o-meter documentation for more information
  6.  */
  7.  
  8. /* Online2NComm.c -- Convert Online-o-Meter style Log File to NComm one */
  9.  
  10. #include <exec/devices.h>
  11. #include <devices/timer.h>
  12. #include <dos/dos.h>
  13. #include <dos/datetime.h>
  14. #include <utility/date.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <proto/timer.h>
  18. #include <proto/utility.h>
  19.  
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. struct Library *TimerBase = NULL;
  25. struct timerequest *tr;
  26. struct RDArgs *args = NULL;
  27. BPTR input = 0,
  28.      output = 0;
  29. UBYTE buffer[100];
  30.  
  31. #define NUMARGS 2
  32. #define TEMPLATE "INPUT,OUTPUT"
  33.  
  34. extern VOID clean_exit(LONG);
  35. extern VOID Convert2NComm(VOID);
  36.  
  37. #ifdef LATTICE
  38. int CXBRK(void) {return 0;}
  39. int chkabort(void) { return(0); }
  40. #endif
  41.  
  42. UBYTE VersTag[] = "\0$VER: Online2NComm 1.0 (" __DATE__ ")";
  43.  
  44. /*------------------------------------------------------------------------*/
  45. /* NAME
  46.  *      DupFileHandle -- Duplicate a Console FileHandle.
  47.  *
  48.  * SYNOPSIS
  49.  *      duplicate = DupFileHandle( fh )
  50.  *      BPTR        DupFileHandle( BPTR );
  51.  *
  52.  * FUNCTION
  53.  *      Duplicates an open Console FileHandle, opening it as MODE_OLDFILE.
  54.  *
  55.  * INPUTS
  56.  *      fh - AmigaDOS FileHandle to Duplicate. MUST be a valid Console
  57.  *           FileHandle, anything else may crash the system or have
  58.  *           undefined results.
  59.  *
  60.  * RESULT
  61.  *      duplicate - another FileHandle to the Console, or NULL if clone
  62.  *                  failed. Must be closed in the same way as any other 
  63.  *                  FileHandle.
  64.  *
  65.  * NOTES
  66.  *      Based on an example in the Amiga GURU book by Ralph Babel.
  67.  */
  68. BPTR DupFileHandle(BPTR fh)
  69. {
  70.   struct MsgPort *old = 
  71.     SetConsoleTask((struct MsgPort *)
  72.            ((struct FileHandle *)BADDR(fh))->fh_Type);
  73.   BPTR duplicate = Open("CONSOLE:",MODE_OLDFILE);
  74.  
  75.   (VOID)SetConsoleTask(old);
  76.  
  77.   return duplicate;
  78. }
  79.  
  80. /*------------------------------------------------------------------------*/
  81. VOID main(int argc, char **argv)
  82. {
  83.   STRPTR result;
  84.   LONG rc;
  85.   LONG argarray[NUMARGS];
  86.   memset(argarray,'\0',sizeof(LONG)*NUMARGS);
  87.  
  88.   if(!Cli())
  89.      exit(RETURN_FAIL);
  90.  
  91.   /* Open timer device for time arithmetic functions */
  92.   if( OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)tr,0) ) {
  93.     Printf("Couldn't open %s.",TIMERNAME);
  94.     exit(RETURN_FAIL);
  95.   }
  96.   TimerBase = (struct Library *)tr->tr_node.io_Device;
  97.  
  98.   /* Process the arguments */
  99.   if(!(args = ReadArgs(TEMPLATE,argarray,NULL))) {
  100.     Printf("Bad Args.\n");
  101.     clean_exit(RETURN_WARN);
  102.   }
  103.  
  104.   /* Sort out input */
  105.   if(!argarray[0]) {
  106.     /* Use Input() */
  107.     input = DupFileHandle(Input());
  108.   }
  109.   else {
  110.     /* Use File */
  111.     if(!(input = Open((STRPTR)argarray[0],MODE_OLDFILE))) {
  112.       Printf("Can't open file %s for input.\n",argarray[0]);
  113.       clean_exit(RETURN_WARN);
  114.     }
  115.   }
  116.  
  117.   /* Sort out output */
  118.   if(!argarray[1]) {
  119.     /* Use Output() */
  120.     output = DupFileHandle(Output());
  121.   }
  122.   else {
  123.     /* Use File */
  124.     if(!(output = Open((STRPTR)argarray[1],MODE_NEWFILE))) {
  125.       Printf("Can't open file %s for output.\n",argarray[1]);
  126.       clean_exit(RETURN_WARN);
  127.     }
  128.   }  
  129.  
  130.   memset(buffer,'\0',sizeof(UBYTE)*100);
  131.     
  132.   /* Read lines and convert */
  133.   while((result = FGets(input,buffer,100)) == buffer) {
  134.     
  135.     /* Do quick check to see if legal line */
  136.     if(strlen(buffer) == 79 &&
  137.        /* Convert */
  138.        buffer[44] == '-' &&
  139.        buffer[66] == ':')
  140.       Convert2NComm();
  141.  
  142.     if(CheckSignal(SIGBREAKF_CTRL_C)) {
  143.       Printf("***Break\n");
  144.       break;
  145.     }
  146.   }
  147.  
  148.   if( !result && (rc = IoErr()) != 0 ) {
  149.     PrintFault(rc,"Error Reading from input file");
  150.     clean_exit(RETURN_WARN);
  151.   }
  152.  
  153.   clean_exit(RETURN_OK);
  154. }
  155.  
  156. /*------------------------------------------------------------------------*/
  157. /* Convert Online-o-Meter style entry to NComm style */
  158. VOID Convert2NComm(VOID)
  159. {
  160.   struct DateTime dt = {{0},FORMAT_DOS,0,NULL,NULL,NULL};
  161.   struct ClockData cd;
  162.   UWORD hours,mins,secs;
  163.   struct timeval tv1 = {0,0},tv2 = {0,0};
  164.   extern char *__montbl[], *__daytbl[];
  165.  
  166.   /* Point to Date & Time Portions and NULL terminate them */
  167.   dt.dat_StrDate = &buffer[42];
  168.   buffer[51] = '\0';
  169.   dt.dat_StrTime = &buffer[52];
  170.   buffer[60] = '\0';
  171.  
  172.   FPutC(output,(LONG)'\n');
  173.   Flush(output);
  174.  
  175.   Write(output,buffer,40);
  176.   Flush(output);
  177.  
  178.   FPuts(output,"\n------------------------------------------------\nLogin:  ");
  179.  
  180.   /* Convert O-o-M date to NComm one */
  181.   if(StrToDate(&dt)) {
  182.  
  183.     tv1.tv_secs = 
  184.       (dt.dat_Stamp.ds_Days * 24 * 3600) + 
  185.     (dt.dat_Stamp.ds_Minute * 60) + 
  186.       (dt.dat_Stamp.ds_Tick / TICKS_PER_SECOND);
  187.  
  188.     Amiga2Date(tv1.tv_secs,&cd);
  189.     FPrintf(output,"%s %s %02lu %02lu:%02lu:%02lu %lu\n",
  190.         __daytbl[cd.wday],
  191.         __montbl[cd.month-1],
  192.         cd.mday,
  193.         cd.hour,
  194.         cd.min,
  195.         cd.sec,
  196.         cd.year);
  197.  
  198.     /* Now Convert Time Elapsed to Time Logged out */
  199.     sscanf(&buffer[61],"%hu:%hu:%hu",&hours,&mins,&secs);
  200.     tv2.tv_secs = secs + ((hours * 3600) + (mins * 60));
  201.  
  202.     AddTime(&tv2,&tv1);
  203.  
  204.     Amiga2Date(tv2.tv_secs,&cd);
  205.     FPrintf(output,"Logout: %s %s %02lu %02lu:%02lu:%02lu %lu\n",
  206.         __daytbl[cd.wday],
  207.         __montbl[cd.month-1],
  208.         cd.mday,
  209.         cd.hour,
  210.         cd.min,
  211.         cd.sec,
  212.         cd.year);
  213.  
  214.     /* Print Elapsed time */
  215.     FPrintf(output,"Time Online: %02lu:%02lu:%02lu\n",
  216.         hours,mins,secs);
  217.   }
  218.   
  219.   Flush(output);
  220. }
  221.  
  222. /*------------------------------------------------------------------------*/
  223. VOID clean_exit(LONG rc)
  224. {
  225.   if(output)
  226.     Close(output);
  227.  
  228.   if(input)
  229.     Close(input);
  230.  
  231.   if(args)
  232.     FreeArgs(args);
  233.  
  234.   if(TimerBase)
  235.     CloseDevice((struct IORequest *)tr);
  236.  
  237.   exit(rc);
  238. }
  239.